package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Pan Rundong
 * @Date 2023-02-02 10:14
 * @Desc 订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 支付结算
     * @Author: Pan Rundong
     * @Date: 2023-02-02 11:26
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param orders
     * @return com.itheima.reggie.common.R<java.lang.String>
     */
    @PostMapping("/submit")
    public R<Orders> order(@RequestBody Orders orders) {
        log.info(orders.toString());
        Orders order = ordersService.submit(orders);
        return R.success(order);
    }

    /**
     * 订单查询
     * @Author: Pan Rundong
     * @Date: 2023-02-02 11:27
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param page
     * @param pageSize
     * @return com.itheima.reggie.common.R<java.util.List>
     */
    @GetMapping("/userPage")
    public R<Page> list(int page, int pageSize) {
        log.info("订单page = {}，pageSize = {}", page, pageSize);
        Page<OrdersDto> pageInfo = ordersService.getPage(page, pageSize);
        return R.success(pageInfo);
    }

    /**
     * 订单分页查询
     * @Author: Pan Rundong
     * @Date: 2023-02-05 21:45
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return com.itheima.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("page = {}, pageSize = {}, beginTime = {}, endTime = {}", page, pageSize, beginTime, endTime);
        Page<OrdersDto> dtoPage = ordersService.getPage(page, pageSize, number, beginTime, endTime);
        return R.success(dtoPage);
    }

    /**
     * 修改订单状态
     * @Author: Pan Rundong
     * @Date: 2023-02-06 9:40
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param ordersDto
     * @return com.itheima.reggie.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody OrdersDto ordersDto) {
        log.info(ordersDto.toString());
        ordersService.updateStatus(ordersDto);
        return R.success("修改成功");
    }
}
